package com.qp.basics.admin.controller;


import com.qp.basics.common.config.InitDataService;
import com.qp.basics.common.datasource.DynamicDataSource;
import com.qp.basics.common.utils.RedisUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/client/assistant")
public class AssistantController extends BaseController {
    @Autowired
    InitDataService initDataService;
    @Autowired
    DynamicDataSource dynamicDataSource;

    @Autowired
    RedisUtils redisUtils;

    @GetMapping("/refreshDB")
    public ResponseEntity refreshDB(@RequestParam String domain) {
        initDataService.refreshDB(domain);
        return success("ok");
    }

    @GetMapping("/refreshAllDB")
    public ResponseEntity refreshAllDB() {
        initDataService.init();
        return success("ok");
    }

    @GetMapping("/listAllDB")
    public ResponseEntity listAllDB() {
        Map<Object, Object> targetDataSources = dynamicDataSource.getTargetDataSources();
        return success(targetDataSources.keySet());
    }

    @GetMapping("testredis")
    public ResponseEntity testredis(@RequestParam(required = false) String k, @RequestParam(required = false) String v) {
        if (StringUtils.isNotEmpty(k)) {
            redisUtils.set(k, v);
        }
        return success(redisUtils.findKeysForPage("*",0,100));
    }

}
